package com.example.gc.myapplication;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    //SharedPreference对象只能读取数据，如果需要存储或者修改数据就需要获得Editor对象并且commit之后才能生效
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        save();
        load();
    }
    private void save(){
        SharedPreferences pref = this.getSharedPreferences("myPref",MODE_PRIVATE);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("name","zhangsan");
        editor.putInt("age", 20);
        editor.putLong("time", System.currentTimeMillis());
        editor.commit();
    }
    private void load(){
        SharedPreferences pref = this.getSharedPreferences("myPref",MODE_PRIVATE);
        System.out.println(pref.getString("name",null));
        System.out.println(pref.getLong("time",0)+"");
    }
}
